//
//  main.m
//  OC06_集合遍历和数组排序
//
//  Created by dllo on 15/12/8.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Person.h"

int main(int argc, const char * argv[]) {
    
#pragma mark - 知识点1 for遍历集合
#if 0
    {
    NSArray *array = @[@"zhang", @"wang", @"li", @"zhao"];
    
    for (int i = 0 ; i < array.count; i++) {
        NSLog(@"%@", array[i]);
    }
    
    NSDictionary *dic = @{@"name" : @"wangqiang", @"age" : @"38", @"sex" : @"m"};
    
    NSArray *keys = [dic allKeys];
    
    for (int i = 0; i < keys.count; i++) {
        NSString *key = keys[i];
        NSString *str = [dic objectForKey:key];
        NSLog(@"%@", str);
    }
    
    NSSet *set = [NSSet setWithObjects:@"zhang", @"wang", @"li", nil];
    
    NSArray *arr = [set allObjects];
    
    for (int i = 0; i < arr.count; i++) {
        NSLog(@"%@", arr[i]);
    }
    }
#endif
    
#pragma mark - 知识点2 NSEnumerator 枚举器
#if 0
    {
        NSArray *array = @[@"zhang", @"wang", @"li"];
        
        /** 数组, 字典, 集合都一个枚举器方法: 返回的是枚举器对象  */
        NSEnumerator *enumerator = [array objectEnumerator];
        
        id object;
        while (object = [enumerator nextObject]) {
            NSLog(@"%@", object);
        }
        
        
        
        /** 反向枚举数组 */
        NSEnumerator *reverse = [array reverseObjectEnumerator];
        
        id object1;
        while (object1 = [reverse nextObject]) {
            NSLog(@"%@", object1);
        }
        
        
        NSDictionary *dic = @{@"name" : @"wangqiang", @"age" : @"30", @"sex" : @"m"};
        NSEnumerator *enumDic = [dic objectEnumerator];
        id objectInDic;
        while (objectInDic = [enumDic nextObject]) {
            NSLog(@"%@", objectInDic);
        }
        
        NSSet *set = [NSSet setWithObjects:@"zhang", @"wang", @"liu", nil];
        NSEnumerator *enumSet = [set objectEnumerator];
        id objectInSet;
        while (objectInSet = [enumSet nextObject]) {
            NSLog(@"%@", objectInSet);
        }
        
        
        
        
    }
#endif
    
#pragma mark - 知识点3 for...in 遍历
#if 0
    {
        /** 
         * 重点:
         * 1. 对于数组, forin 枚举的是对象 
         * 2. 对于字典, forin 枚举的是Key
         * 3. 对于集合, forin 枚举的是对象 
         */
        
//        for (<#type *object#> in <#collection#>) {
//            <#statements#>
//        }
        
        
    }
#endif
    
#pragma mark - 知识点4 数组排序

#pragma mark ** sortedArrayUsingDescriptors
#if 0
    {
        NSArray *array = @[@"zhang", @"wang", @"liu", @"zhao"];
        
        /** 1. 创建排序描述对象 */
        NSSortDescriptor *des = [NSSortDescriptor sortDescriptorWithKey:@"self" ascending:NO];
        
        /** 2. 调用数组的排序方法 */
        NSArray *newArr = [array sortedArrayUsingDescriptors:@[des]];
        
        /** 3. 遍历数组, 输出元素 */
        for (NSString *string in newArr) {
            NSLog(@"%@", string);
        }
        
        
        
        Person *per1 = [[Person alloc] initWithName:@"wangqiang" age:80 sex:@"m"];
        Person *per2 = [[Person alloc] initWithName:@"wangyan" age:90 sex:@"w"];
        Person *per3 = [[Person alloc] initWithName:@"huoheiliu" age:23 sex:@"m"];
        Person *per4 = [[Person alloc] initWithName:@"huogaoshou" age:23 sex:@"w"];
        Person *per5 = [[Person alloc] initWithName:@"yangruidong" age:60 sex:@"w"];
        
        /** 需求: 按照年龄升序排序, 如果年龄相同, 再按照姓名升序排列 */
        NSArray *array2 = @[per1, per2, per3, per4, per5];
        NSSortDescriptor *des2 = [NSSortDescriptor sortDescriptorWithKey:@"age"   ascending:YES];
        NSSortDescriptor *des3 = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
        NSArray *newArr2 = [array2 sortedArrayUsingDescriptors:@[des2, des3]];
        
        for (Person *string2 in newArr2) {
            NSLog(@"name:%@, age:%ld, sex:%@", string2.name, string2.age, string2.sex);
        }
    }
#endif
  
#pragma mark ** sortedArrayUsingSelector
#if 0
    {
        /** 数组中都是字符串对象 */
        NSArray *arr = @[@"zhangsan", @"lisi", @"wangwu"];
        
        /** 
         * @param SEL 方法选择器, @selector(方法名).
         *                 方法的返回值必须是 NSComparisonResult.
         *              注意: 此方法是数组中元素的方法
         */
        NSArray *newArr = [arr sortedArrayUsingSelector:@selector(compare:)];
        
        for (NSString *string in newArr) {
            NSLog(@"%@", string);
        }
        
        NSLog(@"-------------------------------------------------------------------------------");
        /** 数组中是Person对象, 按照name升序排序 */
        Person *per1 = [[Person alloc] initWithName:@"wangqiang" age:80 sex:@"m"];
        Person *per2 = [[Person alloc] initWithName:@"wangyan" age:90 sex:@"w"];
        Person *per3 = [[Person alloc] initWithName:@"huoheiliu" age:23 sex:@"m"];
        Person *per4 = [[Person alloc] initWithName:@"huogaoshou" age:23 sex:@"w"];
        Person *per5 = [[Person alloc] initWithName:@"yangruidong" age:60 sex:@"w"];
        
        NSArray *array2 = @[per1, per2, per3, per4, per5];
        
        NSArray *newArr2 = [array2 sortedArrayUsingSelector:@selector(compareWithNameUP:)];
        for (Person *per in newArr2) {
            NSLog(@"name:%@, age:%ld, sex:%@", per.name, per.age, per.sex);
        }
        
        NSLog(@"-------------------------------------------------------------------------------");
        /** 按照名字降序排序 */
        NSArray *newArr3 = [array2 sortedArrayUsingSelector:@selector(compareWithNameDown:)];
        for (Person *per2 in newArr3) {
            NSLog(@"name:%@, age:%ld, sex:%@", per2.name, per2.age, per2.sex);
        }
    
        NSLog(@"-------------------------------------------------------------------------------");
        /** 按照年龄升序排序 */
        NSArray *newArr4 = [array2 sortedArrayUsingSelector:@selector(compareWithAgeUP:)];
        for (Person *per3 in newArr4) {
            NSLog(@"name:%@, age:%ld, sex:%@", per3.name, per3.age, per3.sex);
        }
        
    }
#endif
    
#pragma mark ** sortedArrayUsingComparator
#if 0
    {
        NSArray *arr = @[@"wangqiang", @"zhang", @"li", @"zhao"];
        NSArray *newArr = [arr sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
            
            return [obj1 compare:obj2];
        }];
        
        for (NSString *string in newArr) {
            NSLog(@"%@", string);
        }
        
        
      NSLog(@"-------------------------------------------------------------------------------");
        
        
        
        Person *per1 = [[Person alloc] initWithName:@"wangqiang" age:80 sex:@"m"];
        Person *per2 = [[Person alloc] initWithName:@"wangyan" age:90 sex:@"w"];
        Person *per3 = [[Person alloc] initWithName:@"huoheiliu" age:23 sex:@"m"];
        Person *per4 = [[Person alloc] initWithName:@"huogaoshou" age:23 sex:@"w"];
        Person *per5 = [[Person alloc] initWithName:@"yangruidong" age:60 sex:@"w"];
        
        NSArray *array2 = @[per1, per2, per3, per4, per5];
        
        /** 按照Person的name升序排序 */
        NSArray *newArr2 = [array2 sortedArrayUsingComparator:^NSComparisonResult(Person*  _Nonnull obj1, Person*  _Nonnull obj2) {
            
            return [obj1.name compare:obj2.name];
            
        }];
        
        for (Person *per in newArr2) {
            NSLog(@"%@", per.name);
        }
       NSLog(@"-------------------------------------------------------------------------------");
        
        /** 按照Person的age降序排序 */
        NSArray *ageDown = [array2 sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
            Person *per1 = obj1;
            Person *per2 = obj2;
            if (per1.age > per2.age) {
                return NSOrderedAscending;
            } else if (per1.age < per2.age) {
                return NSOrderedDescending;
            } else {
                return NSOrderedSame;
            }
        }];
        for (Person *per9 in ageDown) {
            NSLog(@"%ld", per9.age);
        }
        
        
        
        
        
    }
#endif
    
#pragma mark - 知识点4 可变数组排序
#if 0
    {
        NSMutableArray *mArr = @[@"wangqiang", @"zhang", @"li"].mutableCopy;
        
        /** 升序排序 */
  //      [mArr sortedArrayUsingSelector:@selector(compare:)];
        
        [mArr sortUsingSelector:@selector(compare:)];
  
    }
#endif
    
#pragma mark - 时间就是金钱
#if 0
    {
    /** API: NSDate */
    
    
    /** 对100万个样本进行排序, 花费时间? */
    NSMutableArray *arr = [NSMutableArray array];
    for (int i = 0; i < 1000000; i++) {
        NSString *str = [NSString stringWithFormat:@"%ud", arc4random()];
        [arr addObject:str];
    }
    NSDate *startDate = [NSDate date];
    NSLog(@"%@", startDate);
    
//    /** 数组排序 */
//    for (int i = 0; i < arr.count - 1; i++) {
//        for (int j = 0; j < arr.count - i - 1; j++) {
//            if ([arr[j] intValue] > [arr[j + 1] intValue]) {
//                [arr exchangeObjectAtIndex:j withObjectAtIndex:j + 1];
//            }
//        }
//    }
    
    [arr sortedArrayUsingSelector:@selector(compare:)];
    
    
    
    
    NSDate *endDate2 = [NSDate date];
    NSLog(@"%@", endDate2);
    
    /** 计算两个时间对象的时间差 */
    NSLog(@"%f",[endDate2 timeIntervalSinceDate:startDate]);
    }
#endif
    return 0;
}
